package com.example.treehole.service

import com.example.treehole.ui.model.ApiResponse
import com.example.treehole.ui.model.Comment
import com.example.treehole.ui.model.PostDetail
import com.example.treehole.ui.model.PostInfo
import com.example.treehole.ui.model.UserInfo
import okhttp3.MultipartBody
import retrofit2.Response
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.POST
import retrofit2.http.Part

interface ApiService {

    // 发送验证码
    data class VerifyRequest(
        val userEmail: String
    )
    @POST("verify")
    suspend fun sendVerifyCode(@Body verifyRequest: VerifyRequest): Response<ApiResponse<Unit>>

    // 用户注册，注册之后自动登录
    data class RegisterRequest(
        val userEmail: String,
        val password: String,
        val verifyCode: String
    )
    @POST("register")
    suspend fun registerUser(
        @Body registerRequest: RegisterRequest
    ):Response<ApiResponse<UserInfo>>

    // 修改密码
    data class ModifyPassWordRequest(
        val userEmail: String,
        val newPassword: String,
        val verifyCode: String
    )
    @POST("modifyPassword")
    suspend fun modifyPassword(
        @Body modifyPassWordRequest: ModifyPassWordRequest
    ):Response<ApiResponse<Unit>>

    // 密码登录
    data class LoginByPasswordRequest(
        val userEmail: String,
        val password: String
    )
    @POST("loginByPassword")
    suspend fun loginByPassword(
        @Body loginByPasswordRequest: LoginByPasswordRequest
    ):Response<ApiResponse<UserInfo>>

    // 验证码登录
    data class LoginByVerifyRequest(
        val userEmail: String,
        val verifyCode: String
    )
    @POST("loginByVerify")
    suspend fun loginByVerify(
        @Body loginByVerifyRequest: LoginByVerifyRequest
    ):Response<ApiResponse<UserInfo>>

    data class ActionOnPostRequest(
        val postId: Int,
        val userId: Int
    )
    // 点赞内容
    @POST("like")
    suspend fun likePost(
        @Header("token") token:String,
        @Body actionOnPostRequest: ActionOnPostRequest
    ):Response<ApiResponse<Unit>>

    // 取消点赞内容
    @POST("unlike")
    suspend fun unlikePost(
        @Header("token") token: String,
        @Body actionOnPostRequest: ActionOnPostRequest
    ):Response<ApiResponse<Unit>>

    // 收藏内容
    @POST("favorite")
    suspend fun favoritePost(
        @Header("token") token: String,
        @Body actionOnPostRequest: ActionOnPostRequest
    ):Response<ApiResponse<Unit>>

    // 取消收藏内容
    @POST("unfavorite")
    suspend fun unfavoritePost(
        @Header("token") token: String,
        @Body actionOnPostRequest: ActionOnPostRequest
    ):Response<ApiResponse<Unit>>

    data class ActionOnCmtRequest(
        val cmtId: Int,
        val userId: Int
    )
    // 点赞评论
    @POST("likeCmt")
    suspend fun likeCmt(
        @Header("token") token: String,
        @Body actionOnCmtRequest: ActionOnCmtRequest
    ):Response<ApiResponse<Unit>>

    // 取消点赞评论
    @POST("unlikeCmt")
    suspend fun unlikeCmt(
        @Header("token") token: String,
        @Body actionOnCmtRequest: ActionOnCmtRequest
    ):Response<ApiResponse<Unit>>

    // 搜索帖子/分区搜索
    data class SearchPostRequest(
        val query: String,
        val userId: Int,
        val tag: Int
    )
    @POST("search")
    suspend fun searchPost(
        @Header("token") token: String,
        @Body searchPostRequest: SearchPostRequest
    ):Response<ApiResponse<List<PostInfo>>>

    // 上传图片
    @POST("uploadImage")
    suspend fun uploadImage(
        @Header("token") token:String,
        @Part file:MultipartBody.Part
    ):Response<ApiResponse<String>>

    // 发布帖子
    data class CreatePostRequest(
        val userId: Int,
        val title: String,
        val content: String,
        val pics: List<String>,
        val tag: Int
    )
    @POST("createPost")
    suspend fun  createPost(
        @Header("token") token: String,
        @Body createPostRequest: CreatePostRequest
    ):Response<ApiResponse<Int>>

    // 发布评论
    data class CreateCmtRequest(
        val userId: Int,
        val postId: Int,
        val type: Int,   // 对帖子的评论/对评论的评论
        val cmtCmtId: Int,
        val replyUserName: String,
        val content: String
    )
    @POST("createCmt")
    suspend fun createCmt(
        @Header("token") token: String,
        @Body createCmtRequest: CreateCmtRequest
    ):Response<ApiResponse<Comment>>

    // 查看树洞最新页/分区查看树洞
    data class AskNewPostRequest(
        val userId: Int,
        val tagId: Int,
        val page: Int
    )
    @POST("new")
    suspend fun askNewPost(
        @Header("token") token: String,
        @Body askNewPostRequest: AskNewPostRequest
    ):Response<ApiResponse<List<PostInfo>>>

    // 查看树洞热门页
    data class AskHotPostRequest(
        val userId: Int
    )
    @POST("hot")
    suspend fun askHotPost(
        @Header("token") token: String,
        @Body askHotPostRequest: AskHotPostRequest
    ):Response<ApiResponse<List<PostInfo>>>

    // 获取帖子详情
    @POST("content")
    suspend fun askPostDetail(
        @Header("token") token:String,
        @Body actionOnPostRequest: ActionOnPostRequest
    ):Response<ApiResponse<PostDetail>>

    data class GetListRequest(
        val userId: Int,
        val page: Int
    )
    // 查看个人帖子
    @POST("homepage/posts")
    suspend fun getMyPosts(
        @Header("token") token: String,
        @Body getListRequest: GetListRequest
    ):Response<ApiResponse<List<PostInfo>>>

    // 查看个人收藏
    @POST("homepage/favorite")
    suspend fun getMyFavoritePosts(
        @Header("token") token: String,
        @Body getListRequest: GetListRequest
    ):Response<ApiResponse<List<PostInfo>>>

    // 查看个人评论
    @POST("homepage/comment")
    suspend fun getMyComments(
        @Header("token") token: String,
        @Body getListRequest: GetListRequest
    ):Response<ApiResponse<List<Comment>>>

}